package com.bigData.main.TestSocket;

import com.bigData.main.TestHDFS.TestConnectionHDFS;
import com.bigData.main.pojo.Car;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

public class Method {
    //返回8000辆车的信息【cid  came   color  price】
    public List<Car> getCarMessage() {
        //创建集合
        List<Car> list = new ArrayList<Car>();

        for (int i = 1; i <= 5; i++) {
            int a = 30000;
            Car c = new Car();
            c.setCid(Long.valueOf(i));
            c.setCname("bm");
            c.setColor("write");
            c.setPrice(880000);
            c.setStatus("");
            list.add(c);
        }
        return list;
    }

    //数据传送协议动态创建   //Xml
    public String createXml() {
        Method m = new Method();
        List<Car> list = m.getCarMessage();
        //创建文档对象
        Document document = DocumentHelper.createDocument();
        //创建对应的节点
        Element xml = DocumentHelper.createElement("xml");
        //设置跟节点
        document.setRootElement(xml);//将xml最为跟节点

        for (Car c : list) {
            //创建car节点
            Element car = DocumentHelper.createElement("car");
            //创建cid节点
            Element cid = DocumentHelper.createElement("cid");
            //向节点存值--数据类型转换     自动类型转换      包装类过渡型转换
            cid.setText(String.valueOf(c.getCid()));
            Element cname = DocumentHelper.createElement("cname");
            cname.setText(c.getCname());
            Element color = DocumentHelper.createElement("color");
            color.setText(c.getColor());
            Element price = DocumentHelper.createElement("price");
            price.setText(c.getPrice() + "");
            Element status = DocumentHelper.createElement("status");
//            status.setText(c.getStatus() + "");
            //调整节点
            xml.add(car);
            car.add(cid);
            car.add(cname);
            car.add(color);
            car.add(price);
            car.add(status);
        }

        //将xml文档转为字符串
        String str = document.asXML();
        return str;
    }

    //数据解析
    public void passXML(String xml) {
        //将对应的协议字符串转换为xml文档
        try {
            Document document = DocumentHelper.parseText(xml);
            List<Node> car = document.selectNodes("xml/car");
            //遍历
            for (Node n : car) {//一个n代表一个car节点
                String cid = n.selectSingleNode("cid").getText();//取car里的值
                String cname = n.selectSingleNode("cname").getText();
                String color = n.selectSingleNode("color").getText();
                String price = n.selectSingleNode("price").getText();
                String status = n.selectSingleNode("status").getText();
                System.out.println(cid + "，" + cname + "，" + color + "，" + price + "," + status);
                //指定文件
                File f = new File("d://gongfang/car.txt");
                FileWriter w = new FileWriter(String.valueOf(f), true);
                w.write(cid + "，" + cname + "，" + color + "，" + price + "，" + status + "\r\n");
                w.flush();//清空缓存
            }

            File f1 = new File("d://gongfang/car.txt");
            //将数据存储到HDFS分布式文件系统
            TestConnectionHDFS dfs = new TestConnectionHDFS();
            dfs.init();
            dfs.upload("D:\\gongfang\\car.txt", "/myfile_cax/car.txt");
//            f1.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
